// pages/express/express.js
const db=wx.cloud.database()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //选择快递名称
    select:false,
    grade_name:'--请选择--',
    grades: [ '菜鸟驿站','学七快递点', '京东快递','顺丰快递'],

    // 选择宿舍宿舍楼
    decide:false,
    decide_name:'--请选择--',
    hostel:['城市学院学1','城市学院学2','城市学院学3','城市学院学4','城市学院学5','城市学院学6','城市学院学7','城市学院学8','城市学院学9','城市学院学10','城市学院学11','城市学院学12',],


    //宿舍框
    greenhand:false,
    student7:false,
    shunfeng:false,
    jingdong:false,
    //图片
    filePath:"cloud://linjiaan-daidai-1gqqojy38f85b825.6c69-linjiaan-daidai-1gqqojy38f85b825-1303839245/atloal-image/jiahao.png",
    //取件码
    pickupnum:"",
    //图片的fileid
    fileId:"1",
    //订单Id
    orderId:"",
    //取件码的判断判断结果
    txtorder:true,
    //快递收费
    ordermoney:"",
    //判断个人资料是否完善
    textuserdata:false,
    //判断是否有图像
    fileimage:true,
    //用户手机号跟名字
    phone:"",
    name:"",
    //确认页面
    xxxmmm:false,
    sssssssss:false,
    cloudPath:"",
    pulldownImage:"cloud://linjiaan-daidai-1gqqojy38f85b825.6c69-linjiaan-daidai-1gqqojy38f85b825-1303839245/atloal-image/sanjiao.png",
    
  },
  //图片上传
  greenhandImage(){
    wx.chooseImage({
      count: 1,
    }).then(res=>{
      var filePath=res.tempFilePaths[0]
        this.setData({
          filePath:filePath,//临时路径
          fileimage:false
        })
    })
  },

  //上传函数
  upload(cloudPath,filePath){
    wx.cloud.uploadFile({
      cloudPath:cloudPath,
      filePath:filePath,
      success:res=>{
        this.setData({
          fileId:res.fileID,
          filePath:"/images/jiahao.png",
          greenhand:false,
          student7:false,
          shunfeng:false,
          jingdong:false,
          grade_name:'--请选择--',
          decide_name:'--请选择--',
          fileimage:false
        })
        console.log(this.data.fileId)
      }
    })

  },

//点击按钮
  classButton(){
    var that=this

    
   
    that.textuserdata()
    if(that.data.textuserdata){ 
      wx.showModal({
        title: '提示',
        content: '请完善个人信息后才能下单,是否跳转完善信息',
        success (res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/login/userdata/inputUserdata/inputUserdata',
            })
          } else if (res.cancel) {
            console.log(that.data.textuserdata)
          }
        }
      })

    }else{
       

    wx.cloud.callFunction({
      name:"login",
      success(res){
        let openId=res.result.openid
        let cloudPath=openId+Date.now()+".jpg"
        let filePath=that.data.filePath
        //判断取件码
        that.txtorder(that.data.pickupnum)
        that.ordermoney(that.data.pickupnum)
        if(that.data.grade_name=="--请选择--"||that.data.decide_name=="--请选择--"){
          wx.showToast({
            title: '请选择快递或地址',
            icon:'none',
            duration:2000
          })
        }else{
          if(that.data.grade_name=="菜鸟驿站"||that.data.grade_name=="顺丰快递"||that.data.grade_name=="京东快递"){
            console.log(that.data.pickupnum)
           if(that.data.txtorder){
           // 添加数据
           if(that.data.fileimage){
             wx.showToast({
               title: '图片不能为空',
               icon:'none',
               duration:2000
             })
           }else{
            wx.showModal({
              title: '提示',
              content: '代拿该快递需要支付'+that.data.ordermoney+'元，是否提交订单',
              success (res) {
                if (res.confirm) {
                  wx.showLoading({
                    title: '订单提交中',
                    mask:true
                  })
                  that.upload(cloudPath,filePath)
                  that.order(that.data.grade_name,cloudPath,that.data.ordermoney,that.data.pickupnum,that.data.decide_name)
                } else if (res.cancel) {
                  console.log('用户点击取消')
                }
              }
            })
             
           }
        }else{
          wx.showToast({
            title: '取件码格式出错',
            icon:'none',
            duration:2000
          })
        }
        }else{
          // 添加数据
          console.log(that.data.fileimage)
          if(that.data.fileimage){
            wx.showToast({
              title: '图片不能为空',
              icon:'none',
              duration:2000
            })
          }else{
            wx.showModal({
              title: '提示',
              content: '代拿该快递需要支付1元，是否提交订单',
              success (res) {
                if (res.confirm) {
                  wx.showLoading({
                    title: '订单提交中',
                    mask:true
                  })
                  that.upload(cloudPath,filePath)
                  that.order(that.data.grade_name,cloudPath,1,that.data.pickupnum,that.data.decide_name)
                } else if (res.cancel) {
                  console.log('用户点击取消')
                }
              }
            })
          }
        }
        }
      }
    })
    }
    },

    //用户学七确定框
    // xxxmmm(){
    //   var that=this
    //   that.upload(that.data.cloudPath,that.data.filePath)
    //   that.order(that.data.grade_name,that.data.cloudPath,that.data.ordermoney,that.data.pickupnum,that.data.decide_name)
    // },


    // 菜鸟确定框

  
  //快递的下拉点击事件
  bindShowMsg() { 
    this.setData({
    select: !this.data.select
  })
},

  //宿舍地址的下拉点击事件
  bindShowMsg2() { 
    this.setData({
    decide: !this.data.decide
  })

},
/**
* 快递已选下拉框 */
  mySelect(e) {
    console.log(e)      
    var name = e.currentTarget.dataset.name    
    if(name=="菜鸟驿站"){
     this.setData({greenhand:true,student7:false,shunfeng:false,jingdong:false})
    }else if(name=="学七快递点"){
      this.setData({student7:true,greenhand:false,shunfeng:false,jingdong:false})
    }else if(name=="顺丰快递"){
      this.setData({shunfeng:true,greenhand:false,student7:false,jingdong:false})
    }else if(name=="京东快递"){
      this.setData({jingdong:true,greenhand:false,student7:false,shunfeng:false})
    }
    this.setData({
      grade_name: name,
      select: false
    })
  },
// 宿舍已选下拉框
  mydecide(e) {
    console.log(e)      
    var name = e.currentTarget.dataset.name    
    this.setData({
      decide_name: name,
      decide: false
    })
  },
  //取件码的获取
  pickupnum:function(e){
    var that=this
    that.setData({
      pickupnum:e.detail.value
    })
  },

  //取件码格式的判断
  txtorder(pickupnum){
    this.setData({
      txtorder:true
     })
    var str=pickupnum.split('-')
    if(str.length!=3){
      this.setData({
        txtorder:false
       })
    }else{
      for(var i=0;i<str.length;i++){
      for(var j=0;j<str[i].length;j++){
        if(str[i].charAt(j)>=0&&str[i].charAt(j)<=9){}
        else{
            this.setData({
            txtorder:false
           })
           break
        }
      }
    }
    }
    if(this.data.txtorder){
      if(str[1].length!=1||str[2].length!=4){
        this.setData({
          txtorder:false
         })
      }
    }
  },

  //根据取件码来判断下单的钱数
  ordermoney(pickupnum){
    var str=pickupnum.split('-')
    if(str[1]==1||str[1]==2||str[1]==5||str[1]==6){
      this.setData({
        ordermoney:1
      })
    }else if(str[1]==3||str[1]==7||str[1]==8||str[1]==4){
      this.setData({
        ordermoney:3
      })
    }else{
      this.setData({
        ordermoney:5
      })
    }
  },

  //订单记录添加
  order(expressadd,image,money,pickupnum,useradd){
    var that=this
    db.collection('express').add({
      data:{
        expressadd:expressadd,
        image:image,
        money:money,
        pickupnum:pickupnum,
        useradd:useradd,
        time: Date.parse(new Date())/1000,
        state:"未取",
        caballero:""
      }
    }).then(res=>{
      that.setData({
        orderId:res._id
      })
      wx.hideLoading()
      wx.showToast({
        title: '订单提交成功',
        icon:'success',
        duration:2000
      })
    })
  },

  //判断信息是否完善
  textuserdata(){
    var that=this
    wx.cloud.callFunction({
      name:"login",
      success(ress){
        let openId=ress.result.openid
        db.collection('user').where({
          openid:openId
         }).get().then(res=>{
          if(res.data[0].realname==""||res.data[0].phone==""){
            that.setData({
            textuserdata:true,
            phone:res.data[0].phone,
            name:res.data[0].name
        })
      }else{
         that.setData({
          textuserdata:false
        })
      }
    })
      }
    })
    
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.textuserdata()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})